//
//  ViewController.m
//  MD5加密-解密-作业
//
//  Created by mafeng on 16/8/16.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import <CommonCrypto/CommonCrypto.h>

@interface ViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *enCodeTextField;

@property (weak, nonatomic) IBOutlet UITextField *deCodeTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

 // 加密方法
- (IBAction)enCode:(id)sender {
    
    NSString *str = _enCodeTextField.text;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = @"http://tool.zzblo.com/api/md5/encrypt";
    NSDictionary *parameters = @{@"text": str};
    [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求数据成功");
        NSLog(@"%@", responseObject);
        NSString *secret32 = responseObject[@"secret16"];
        NSLog(@"%@", secret32);
        _deCodeTextField.text = secret32;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}


// 转化为MD5
- (IBAction)transfromToMD5:(id)sender {
    // c 代码 MD5 加密
    NSString *string = _deCodeTextField.text;
    const char *str = [string UTF8String];
    CC_LONG len = (CC_LONG)strlen(str);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    CC_MD5(str, len, bytes);
     NSMutableString *finalString = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < 16; i++) {
       [finalString appendFormat:@"%02x", bytes[i]];
    }
    _deCodeTextField.text = finalString;
}

// 解密方法
- (IBAction)deCode:(id)sender {
    NSString *deCodeString =  _deCodeTextField.text;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = @"http://tool.zzblo.com/api/md5/decrypt";
    NSDictionary *parameters = @{@"secret" : deCodeString};
    [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"解密成功");
        NSLog(@"%@", responseObject);
        _enCodeTextField.text = responseObject[@"text"];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"❤️");
    [textField resignFirstResponder];
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
